<template>
  <div class="app-container">
    <div class="item2">
      <div class="header">
        <div class="title1">
          <i class="el-icon-data-analysis"></i><span>笼车信息查询</span>
        </div>
      </div>
      <div class="formCenter">
        <el-form
          :model="queryParams"
          size="large"
          class="search-form"
          :inline="true"
          ref="queryForm"
          :rules="formRules"
          label-width="68px"
        >
          <div class="form-row">
            <el-form-item label="笼车号:" prop="boxNo" class="form-item">
              <el-input
                v-model="queryParams.boxNo"
                placeholder="请输入笼车号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <div class="button-group">
              <el-button
                type="primary"
                icon="el-icon-search"
                size="small"
                @click="handleQuery"
                >搜索</el-button
              >
              <!-- <el-button
              type="primary"
              icon="el-icon-refresh"
              size="medium"
              @click="handleCarrelease"
              >一键释放</el-button
            > -->
            </div>
          </div>
        </el-form>
      </div>
      <el-table
        :data="tableData"
        border
        height="680"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        :header-cell-style="{ background: '#1890ff', color: '#ffffff' }"
      >
        <el-table-column align="center" prop="boxNo" label="笼车号">
        </el-table-column>
        <el-table-column align="center" prop="billNo" label="单号">
        </el-table-column>
        <!-- <el-table-column align="center" prop="routeLine" label="routeLine">
        </el-table-column> -->
        <!-- <el-table-column align="center" prop="locaTionId" label="位置">
        </el-table-column> -->
        <!-- <el-table-column align="center" prop="agvStatus" label="agv状态">
        </el-table-column> -->
        <!-- <el-table-column align="center" prop="addWho" label="操作人">
        </el-table-column> -->
        <!-- <el-table-column align="center" prop="addTme" label="操作时间">
        </el-table-column> -->
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleRelease(scope.row)"
              >释放笼车</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="handleQuery"
        />
      </div>
    </div>
    <div class="footer">
      <el-dialog
        title="操作验证"
        :visible.sync="centerDialogVisible"
        width="80%"
        left
        :close-on-click-modal="false"
      >
        <el-form
          :model="passForm"
          status-icon
          :rules="passrules"
          ref="passForm"
          label-width="60px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="account">
            <el-input
              v-model="passForm.account"
              placeholder="请输入账号"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model="passForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getagvList,
  getagvedit,
  getagvReleaseCar,
} from "@/api/subSystem/continer";
import { getcheckCarPassword } from "@/api/subSystem/continer";
import { debounce } from "@/utils/index";
export default {
  data() {
    // var passwordError = (rule, value, callback) => {
    //   if (!value) {
    //     return callback(new Error("密码不能为空"));
    //   } else{
    //     console.log("1231");
    //     getcheckCarPassword({
    //       configValue: value,
    //       configKey: this.passForm.account,
    //     }).then((res) => {
    //       if (res.msg == "car") {
    //         callback();
    //       } else if (res.msg == "false") {
    //         callback(new Error("账号或密码错误"));
    //       }
    //     });
    //   }
    // };
    // var accountError = (rule, value, callback) => {
    //   if (!value) {
    //     return callback(new Error("账号不能为空"));
    //   } else {
    //     getcheckCarPassword(value).then((res) => {
    //       if (res.msg == "car") {
    //         callback();
    //       } else if (res.msg == "false") {
    //         callback(new Error("账号错误"));
    //       }
    //     });
    //   }
    // };
    return {
      // 笼车释放变量
      releaseData: 0,
      // 密码验证
      centerDialogVisible: false,
      passForm: {
        pass: null,
        account: null,
      },
      passrules: {
        // pass: [{ validator: passwordError, trigger: "blur" }],
        pass: [{ required: true, message: "请输入密码", trigger: "blur" }],
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
      },
      // 总条数
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        boxNo: "1V",
      },
      formRules: {
        boxNo: [{ required: false, message: "请输入笼车号", trigger: "blur" }],
      },
      tableData: [],
      loading: false,
      // 笼车号传参
      boxNumber: null,
    };
  },
  mounted() {
    this.handleQuery();
  },
  methods: {
    // goBack() {
    //   this.$router.push("/subSystem1");
    // },
    handleQuery() {
      this.$refs.queryForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          getagvList(this.queryParams).then((res) => {
            this.tableData = res.rows;
            this.total = res.total;
            this.loading = false;
          });
        }
      });
    },
    handleRelease(row) {
      this.boxNumber = row.boxNo;
      console.log(111, row.boxNo);
      this.centerDialogVisible = true;
      this.passForm.pass = null;
      this.passForm.account = null;
      this.releaseData = 1;
    },
    handleCarrelease() {
      this.centerDialogVisible = true;
      this.passForm.pass = null;
      this.passForm.account = null;
      this.releaseData = 2;
    },
    submit() {
      this.$refs.passForm.validate((valid) => {
        if (valid) {
          getcheckCarPassword({
            configValue: this.passForm.pass,
            configKey: this.passForm.account,
          }).then((res) => {
            if (res.msg == "car") {
              // console.log(this.boxNumber,this.passForm.account);
              if (this.releaseData == 1) {
                console.log(this.releaseData);
                getagvedit({
                  boxNo: this.boxNumber,
                  addWho: this.passForm.account,
                }).then((res) => {
                  console.log(res);
                  this.handleQuery();
                });
              } else if (this.releaseData == 2) {
                console.log(this.releaseData);
                getagvReleaseCar().then((res) => {
                  console.log(res);
                  this.handleQuery();
                });
              }

              this.centerDialogVisible = false;
            } else if (res.msg == "false") {
              this.$modal.msgError("账号或密码错误");
            }
          });
        }
      });
    },
    // submit(formName) {
    //   this.$refs[formName].validate((valid) => {
    //     if (valid) {
    //       // console.log(111,this.boxNumber,this.passForm.account);
    //       // getagvedit({
    //       //   boxNo: this.boxNumber,
    //       //   addWho: this.passForm.account,
    //       // }).then((res) => {
    //       //   console.log(res);
    //       //   this.handleQuery();
    //       // });
    //       // this.centerDialogVisible = false;
    //     }
    //   });
    // },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  .el-icon-data-analysis::before {
    color: #00d9ff;
    margin-right: 8px;
  }
  ::v-deep .el-dialog:not(.is-fullscreen) {
    margin-top: 20vh !important;
  }
  ::v-deep .el-pagination{
    width: 100%;
  }
  .title1 {
    font-size: 16px;
    // margin-bottom: 15px;
    color: #808080;
    width: 170px;
    padding: 5px;
    // border-bottom: 1px dotted #8ddfff;
  }
  .header {
    margin-bottom: 8%;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 220px;
      margin-right: 20px;
    }
  }
  .formCenter {
    width: 100%;
    height: 62px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 20px;
    .search-form {
      width: 100%;
      .form-row {
        display: flex;
        align-items: center;
        flex-wrap: nowrap;
        .form-item {
          flex: 1;
          ::v-deep .el-input {
            width: 100%;
            .el-input__inner {
              height: 36px;
              line-height: 36px;
            }
          }
          ::v-deep .el-form-item__label {
            font-size: 14px;
            padding-right: 5px;
          }
        }
        .button-group {
          white-space: nowrap; // 按钮文字不换行

          .el-button {
            margin-left: 0;
            height: 40px;
            padding: 0 15px;

            // 移动端按钮图标优化
            i {
              margin-right: 3px;
            }
          }
        }
      }
    }
  }
  .footer {
    margin-top: 20px;
    width: 100%;
    // min-height: 200px;
  }
}
</style>